package it.unibo.contact.convoyCruiseControlSystem;

import it.unibo.contact.platformuv.RunTimeKb;
import it.unibo.convoycruisecontrol.boatsubsystem.AbstractCommandBoat;
import it.unibo.convoycruisecontrol.boatsubsystem.DashboardManager;
import it.unibo.convoycruisecontrol.boatsubsystem.IBoatFactory;
import it.unibo.convoycruisecontrol.boatsubsystem.IDashboard;
import it.unibo.convoycruisecontrol.boatsubsystem.SendingNavigationInfo;
import it.unibo.convoycruisecontrol.boatsubsystem.SensingBoatStatus;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.ControlPanel;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.DistanceSupervisor;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.IFlagDisplay;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.IFlagDisplayFactory;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.MapManager;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.StatusSupervisor;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.TimingSupervisor;
import it.unibo.convoycruisecontrol.simulation.Simulator;
import it.unibo.java.location.JavaLocationDeviceFactory;
import it.unibo.location.ILocationDeviceFactory;
import it.unibo.location.LocationDevice;
import it.unibo.mycruise.boat.BoatFactory;
import it.unibo.mycruise.boat.Dashboard;
import it.unibo.mycruise.boat.presentation.swing.DashboardFrame;
import it.unibo.mycruise.boat.presentation.swing.DashboardView;
import it.unibo.mycruise.chief.FlagDisplayFactory;
import it.unibo.mycruise.chief.presentation.swing.ChiefFrame;
import it.unibo.mycruise.chief.presentation.swing.ControlPanelController;
import it.unibo.mycruise.chief.presentation.swing.ControlPanelView;
import it.unibo.mycruise.simulation.SimulatorBoatFactory;
import it.unibo.mycruise.simulation.SimulatorControlPanel;
import it.unibo.mycruise.simulation.presentation.swing.SimulatorController;
import it.unibo.mycruise.simulation.presentation.swing.SimulatorFrame;
import it.unibo.mycruise.simulation.presentation.swing.SimulatorView;
import it.unibo.util.InformationConverter;
import it.unibo.util.PrologInformationConverter;

public class ConvoyCruiseControlSIMULATOR_Boat1 extends ConvoyCruiseControlSystemMain 
{
	static int N = 1;
	static int DT = 5000;
	static int DD = 1000;
	static int DMIN = 500;
	static int R = 2;
	static int a = 3;
	
	LocationDevice locDev;
	ILocationDeviceFactory locationDeviceFactory = new JavaLocationDeviceFactory();
	
	InformationConverter informationConverter = new PrologInformationConverter();

	//Boats
	IBoatFactory boatFactory = new SimulatorBoatFactory();
	AbstractCommandBoat[] boats;
	DashboardManager[] dashManagers;
	SendingNavigationInfo[] sendingNavInfo;
	SensingBoatStatus[] sensingBoatsStatus;
	DashboardView[] dashViews;
	
	//Simulator
	SimulatorView[] simViews;

	public static void main(String[] args) 
	{
		ConvoyCruiseControlSIMULATOR_Boat1 system = new ConvoyCruiseControlSIMULATOR_Boat1();
		system.doJob();
	}

	@Override
	protected void start() 
	{
		for(int i = 0; i < N; i++)
		{
			boats[i].start();
			dashManagers[i].start();
			sendingNavInfo[i].start();
		}
	}


	@Override
	protected void configureSubjects() 
	{
		locDev = locationDeviceFactory.createBestDevice();
		initBoatSubjects();
	}

	private void initBoatSubjects()
	{
		try
		{
			boats = new AbstractCommandBoat[N];
			dashManagers = new DashboardManager[N];
			sendingNavInfo = new SendingNavigationInfo[N];
			sensingBoatsStatus = new SensingBoatStatus[N];
			dashViews = new DashboardView[N];
			simViews = new SimulatorView[N];

			for(int i = 0; i < N; i++)
			{
				// init components
				boats[i] = boatFactory.createBoat(locDev, 1);
				boats[i].setName("boat"+1);
				boats[i].setEnv(null);
				boats[i].initInputSupports();

				IDashboard dash = boatFactory.createDashboard();
				dashManagers[i] = new DashboardManager(dash, boats[i].getNavigationInfo(), locDev, informationConverter);
				dashManagers[i].setName("dashboardManager"+1);
				dashManagers[i].setEnv(null);
				dashManagers[i].initInputSupports();

				sendingNavInfo[i] = new SendingNavigationInfo(boats[i].getNavigationInfo(), informationConverter);
				sendingNavInfo[i].setName("sendingNavigationInfo"+1);
				sendingNavInfo[i].setEnv(null);
				sendingNavInfo[i].initInputSupports();

				sensingBoatsStatus[i] = new SensingBoatStatus(informationConverter);
				sensingBoatsStatus[i].setName("sensingBoatStatus"+1);
				sensingBoatsStatus[i].setEnv(null);
				sensingBoatsStatus[i].initInputSupports();

				boats[i].getStatusBoat().addBoatStatusListener(sensingBoatsStatus[i]);

				SimulatorController simController = new SimulatorController(new Simulator(boats[i]));
				
				// init GUI
				dashViews[i] = new DashboardView((Dashboard) dash);
				simViews[i] = new SimulatorView(simController);
				SimulatorFrame simFrame = new SimulatorFrame(simViews[i], dashViews[i]);
				
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}

